package Shraglecom;

use WWW::Mechanize;
use HTTP::Cookies;

sub upload
{
    my $local_file = shift;
	my $hoster_config = shift;

    my $mech = WWW::Mechanize->new();

    my $cookie_jar = HTTP::Cookies->new(
            file => $::spreader_cookie_dir."shraglecom.dat",
            autosave => 1,
        );

    $mech->cookie_jar($cookie_jar);

    my $textbox_content =  "ftp://".$::spreader_ftp_user.":".$::spreader_ftp_pwd."\@".$::spreader_ftp_ip.$::spreader_ftp_path.$local_file."\n";

    $mech->get("http://www.shragle.com/index.php?p=login");

    if($mech->content =~ /Login/)
    {
        $mech->form_number(1);
        $mech->field("username" => $hoster_config->{'username'});
        $mech->field("password" => $hoster_config->{'password'});
        $mech->field("cookie" => "1");
        $mech->click("submit");	
    }

    $mech->get("http://www.shragle.com/index.php?p=remote");

    $mech->form_number(1);
    $mech->field("userID" => $mech->value("userID",1));
    $mech->field("password" => $mech->value("password",1));
    $mech->field("lang" => $mech->value("lang",1));
    $mech->field("urls" => $textbox_content);
    $mech->submit();

    my $in_process = 1;

    while($in_process)
    {
        $mech->get("http://www.shragle.com/index.php?cat=user&p=files");

        if($mech->content =~ m!(http://.*?speedtest\.zip)!)
        {
            $in_process = 0;
            return $1;
        }
        else
        {
            print "Noch nicht abgeschlossen. Warte ".$hoster_config->{'sleep_linkfetch'}." Sekunden.\n";
            sleep($hoster_config->{'sleep_linkfetch'});
        }
    }
}
1;